package com.mysdnu.mysdnu_android;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu.OnCloseListener;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu.OnOpenListener;
import com.mysdnu.adapter.UserInfoAdapter;
import com.mysdnu.customview.TitleBarView;

public class MainActivity extends Activity {
	private TitleBarView titleBarView;
	private Context context;
	private SlidingMenu slidingMenu;
	private ListView listView_user;
	private List<String> list_userInfo = new ArrayList<String>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		context = this;
		initView();
		initMenu();
		initListener();
		initTab();
	}

	private void initTab() {
		// TODO Auto-generated method stub

	}

	private void initMenu() {
		slidingMenu = new SlidingMenu(context);
		slidingMenu.setMode(slidingMenu.LEFT);
		slidingMenu.setMenu(R.layout.layout_user);
		// 滑动菜单显示的宽度(屏幕宽度的多少被滑动菜单所占据)
		slidingMenu
				.setBehindWidth(getResources().getDisplayMetrics().widthPixels * 4 / 5);
		slidingMenu.attachToActivity(MainActivity.this,
				SlidingMenu.SLIDING_WINDOW);
		listView_user = (ListView) slidingMenu
				.findViewById(R.id.listView_userInfo);
		String[] arr = getResources().getStringArray(R.array.user_info);
		for (int i = 0; i < arr.length; i++) {
			list_userInfo.add(arr[i]);
		}
		UserInfoAdapter adapter = new UserInfoAdapter(context, list_userInfo);
		listView_user.setAdapter(adapter);
	}

	private void initView() {
		// TODO Auto-generated method stub
		titleBarView = (TitleBarView) findViewById(R.id.titleBarView);
		titleBarView.setMenuVisibility(View.INVISIBLE);
	}

	private void initListener() {
		// TODO Auto-generated method stub
		titleBarView.setUserImageClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				slidingMenu.toggle();
			}
		});
		slidingMenu.setOnOpenListener(new OnOpenListener() {

			@Override
			public void onOpen() {
				// TODO Auto-generated method stub
				titleBarView.setUserImageVisibility(View.INVISIBLE);
			}
		});
		slidingMenu.setOnCloseListener(new OnCloseListener() {

			@Override
			public void onClose() {
				// TODO Auto-generated method stub

				titleBarView.setUserImageVisibility(View.VISIBLE);
			}
		});
		listView_user.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Toast.makeText(context, list_userInfo.get(position), 0).show();
			}
		});
		titleBarView.setMenuClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

			}
		});
	}
}
